/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package mtt.menu.mainmenu;

import hmvc.j2me.events.CloseApplicationEvent;
import hmvc.j2me.events.IEvent;
import hmvc.j2me.view.AView;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

import mtt.menu.events.ShowApplicationInfo;
import mtt.menu.events.ShowApplicationOptionEvent;
import mtt.menu.events.ShowCalendarEvent;
import mtt.menu.events.ShowMarksEvent;

/**
 * 
 * 
 * @author jens.meiss
 */
public class MainMenuView extends AView {
	
	private static final Command commandExit = new Command("Beenden",
			Command.EXIT, 0);;

	private static final Command commandSelect = new Command("Anzeigen",
			Command.ITEM, 0);
	
	private final List listMenu = new List("MTT-Hauptmenü", List.IMPLICIT);

	public void handleEvent(IEvent event) {
		// TODO Auto-generated method stub

	}

	public void terminate() {
		// TODO Auto-generated method stub

	}

	public void init() {
		// TODO Auto-generated method stub

	}

	public void registerEvents() {
		/*
		 * add menu entries
		 */
		listMenu.insert(0, "Kalender", null);
		
		listMenu.insert(1, "Noten", null);
		
		listMenu.insert(2, "Optionen", null);
		
		listMenu.insert(3, "Info", null);

		listMenu.addCommand(commandSelect);
		listMenu.addCommand(commandExit);
		
		listMenu.setCommandListener(this);
		
		setDisplayAble(listMenu);
	}
	
	public void commandAction(Command command, Displayable displayAble) {
		/*
		 * Buttons abfragen
		 */
		if (command == commandExit) {
			getController().fireEventUp(new CloseApplicationEvent());
			return;
		} 
//		else if (command == commandSelect) {
//			
//			return;
//		}
		
		/*
		 * Listeneintrag ausgewählt
		 */
		int selectedIndex = listMenu.getSelectedIndex();

		switch (selectedIndex) {
		case 0:
			getController().fireEventUp(new ShowCalendarEvent());
			break;
		case 1:
			getController().fireEventUp(new ShowMarksEvent());
			break;
		case 2:
			getController().fireEventUp(new ShowApplicationOptionEvent());
			break;
		case 3:
			getController().fireEventUp(new ShowApplicationInfo());
			break;
		}
	}
}
